<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * This is the model class for table "menu".
 *
 * @property integer $id
 * @property string $name
 * @property string $route
 * @property integer $parent_id
 * @property integer $level
 * @property integer $sort
 * @property string $description
 */
class Menu extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'menu';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parent_id', 'level', 'sort'], 'integer'],
            [['description'], 'string'],
            [['name', 'route'], 'string', 'max' => 50],
            [['name','route'],'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '菜单名称',
            'route' => '菜单路由',
            'parent_id' => '上级菜单ID',
            'level' => '菜单的级别',
            'sort' => '排序',
            'description' => '菜单的描述',
        ];
    }


    /**
     * @return string
     */
    public static function getParentInfo(){
        $menuinfo = Menu::find()->asArray()->all();
        //$rs =ArrayHelper::map($menuinfo,'id','name');
        //添加顶级菜单
        //var_dump($menuinfo);exit;
        /*

         */
        $menuinfo[]=['id'=>0,'name'=>'顶级菜单'];
        //$rs['0']='顶级菜单';
//        return $rs;
//        return Json::encode($rs);
        return Json::encode($menuinfo);
    }


    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChildren(){
        return $this->hasMany(Menu::className(),['id'=>'parent_id']);
    }


}
